<script lang="ts">
export default {
  name: 'enterpriseList',
  description: '企业用户管理',
};
</script>
<script setup lang="ts">
import { nextTick, onMounted, reactive, ref, toRefs } from 'vue';

import { Search, Plus, Edit, Refresh, Delete } from '@element-plus/icons-vue';
import { ElForm, ElMessage, ElMessageBox } from 'element-plus';
import {
  EnterpriseType,
  EnterpriseTypeQuery,
} from '@/api/occupant/enterprise/types';
import {
  casualEntrepreneurPages,
  casualPersonageAdd,
  casualEntrepreneurEdit,
  casualEntrepreneurDel,
} from '@/api/occupant/enterprise';
import {
  casualEntrepreneurItempages
} from '@/api/occupant/subuser'
import subDetail from './subDetail.vue'

const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);

const state = reactive({
  loading: false,
  // 选中ID数组
  ids: [],
  deptOptions: [],
  dialog: {
    title: '',
    visible: false,
  },
  subDialog: {
    title: '',
    visible: false,
  },
  params:{
    id : String
  },
  dataList: [] as EnterpriseType[],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  } as EnterpriseTypeQuery,
  subuserQueryParams: {
    pageNum: 1,
    pageSize: 10,
  } as EnterpriseTypeQuery,
  total: 0,
  formData: {} as EnterpriseType,
  rules: {
    name: [{ required: true, message: '请填写用户名称', trigger: 'blur' }],
    // delegate: [{ required: true, message: '请填写法人代表', trigger: 'blur' }],
    // taxNo: [{ required: true, message: '请填写统一信用代码', trigger: 'blur' }],
    // mobile: [{ required: true, message: '请填写手机号码', trigger: 'blur' }],
    // email: [{ required: true, message: '请填写邮箱', trigger: 'blur' }],
    // address: [{ required: true, message: '请填写地址', trigger: 'blur' }],
    // idCard: [{ required: true, message: '请填写身份证', trigger: 'blur' }],
  },
  viewSubuserVisible:false,
  viewSubuserLoading:false,
  subuserDataList:[]as EnterpriseType[]
});

const { ids,params, loading, dataList, queryParams, formData, rules, dialog,subDialog, total, viewSubuserVisible,subuserDataList, viewSubuserLoading, subuserQueryParams } =
  toRefs(state);
function handleQuery() {
  loading.value = true;
  casualEntrepreneurPages(state.queryParams).then(({ data }) => {
    dataList.value = data.list as any;
    total.value = data.total;
    loading.value = false;
  });
}
function addSub(){
  state.subDialog = {
    title:'新增',
    visible:true
  }
}
function handleSelectionChange(selection: any) {
  state.ids = selection.map((item: any) => item.id);
}

/**
 * 重置
 */
function resetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}

/**
 * 添加
 */
async function handleAdd(row: any) {
  dialog.value = {
    title: '添加个人用户',
    visible: true,
  };
}
/**
 * 查看子用户
 */
function viewSubuser(params:any) {
  state.params = params
  state.viewSubuserLoading =true
  state.viewSubuserVisible =true
  console.log(state.params)
  console.log(state.params.id)
  entrepreneurItempages( state.params.id )
}

function entrepreneurItempages( id:any  ){
  casualEntrepreneurItempages({ ...subuserQueryParams.value, id:id}).then(({data})=>{
    subuserDataList.value = data.list as any
    state.viewSubuserLoading =false
  })
}
/**
 * 修改
 */
async function handleUpdate(row: any) {
  state.dialog = {
    title: '修改个人用户',
    visible: true,
  };
  nextTick(() => {
    state.formData = Object.assign({}, row);
  });
}

/**
 * 删除
 */
function handleDelete(row: any) {
  const ids = [row.id || state.ids].join(',');
  if (!ids) {
    ElMessage.warning('请勾选删除项');
    return;
  }

  ElMessageBox.confirm(`确认删除已选中的数据项?`, '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      casualEntrepreneurDel(ids).then((res) => {
        ElMessage.success('删除成功');
        resetQuery();
      });
    })
    .catch(() => ElMessage.info('已取消删除'));
}

/**
 * 确认
 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      let parmas = Object.assign({}, formData.value);
      if (formData.value.id) {
        casualEntrepreneurEdit(parmas).then(() => {
          ElMessage.success('编辑成功');
          closeDialog();
          resetQuery();
        });
      } else {
        casualPersonageAdd(parmas).then(() => {
          ElMessage.success('新增成功');
          closeDialog();
          resetQuery();
        });
      }
    }
  });
}
/**
 * 关闭弹窗
 **/
function closeDialog() {
  dialog.value.visible = false;
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
}

onMounted(() => {
  handleQuery();
});
</script>
<template>
  <div class="app-container">
    <div class="search">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="企业全称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入企业全称" @keyup.enter="handleQuery" />
        </el-form-item>

        <el-form-item label="企业编码" prop="code">
          <el-input v-model="queryParams.code" placeholder="请输入企业编码" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="法人代表" prop="delegate">
          <el-input v-model="queryParams.delegate" placeholder="请输入法人代表" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="统一信用代码" prop="taxNo">
          <el-input v-model="queryParams.taxNo" placeholder="请输入统一信用代码" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button class="filter-item" type="primary" :icon="Search" @click="handleQuery">
            搜索
          </el-button>
          <el-button :icon="Refresh" @click="resetQuery"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card>
      <!--toolbar-->
      <template #header>
        <el-button type="success" :icon="Plus" @click="handleAdd">新增</el-button>
        <!--        <el-button-->
        <!--          type="danger"-->
        <!--          :icon="Delete"-->
        <!--          @click="handleDelete"-->
        <!--          :disabled="ids.length === 0"-->
        <!--        >删除-->
        <!--        </el-button>-->
      </template>

      <!--table-->
      <el-table v-loading="loading" :data="dataList" row-key="id" default-expand-all :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="name" label="企业全称" min-width="200" />
        <el-table-column prop="code" label="企业编码" width="200" />
        <el-table-column prop="delegate" label="法人代表" width="140" />
        <el-table-column prop="taxNo" label="统一信用代码" width="140" />
        <el-table-column prop="mobile" label="手机号码" width="140" />
        <el-table-column prop="email" label="邮箱" width="200" />
        <el-table-column prop="contractId" label="合同id" width="200" />
        <el-table-column prop="bankCardId" label="银行id" width="200" />
        <el-table-column prop="address" label="地址" width="140" />
        <el-table-column prop="idCard" label="身份证" width="200" />
        <el-table-column prop="delegateImgGh" label="营业执照" width="170" >
          <template #default="scope">
            <el-image v-if="scope.row.licenseImg" style="width: 140px; height: 100px" :src="'https://lhyg.hollwingroup.com/prod-api/file/download?fileId='+scope.row.licenseImg" fit="scale-down" :preview-src-list="['https://lhyg.hollwingroup.com/prod-api/file/download?fileId='+scope.row.licenseImg]" :preview-teleported="true" hide-on-click-modal/>
            <div v-else style="width: 140px;
              height: 100px;
              text-align: center;
              display: flex;
              align-items: center;
              justify-content: center;
              background-color: white;">无</div>
          </template>
        </el-table-column>
        <el-table-column prop="delegateImgRx" label="证件图片人像" width="170" >
          <template #default="scope">
            <el-image v-if="scope.row.delegateImgRx" style="width: 140px; height: 100px" :src="'https://lhyg.hollwingroup.com/prod-api/file/download?fileId='+scope.row.delegateImgRx" fit="scale-down" :preview-src-list="['https://lhyg.hollwingroup.com/prod-api/file/download?fileId='+scope.row.delegateImgRx]" :preview-teleported="true" hide-on-click-modal/>
            <div v-else style="width: 140px;
              height: 100px;
              text-align: center;
              display: flex;
              align-items: center;
              justify-content: center;
              background-color: white;">无</div>
          </template>
        </el-table-column>
        <el-table-column prop="delegateImgGh" label="证件图片国徽" width="170" >
          <template #default="scope">
            <el-image v-if="scope.row.delegateImgGh" style="width: 140px; height: 100px" :src="'https://lhyg.hollwingroup.com/prod-api/file/download?fileId='+scope.row.delegateImgGh" fit="scale-down" :preview-src-list="['https://lhyg.hollwingroup.com/prod-api/file/download?fileId='+scope.row.delegateImgGh]" :preview-teleported="true" hide-on-click-modal/>
            <div v-else style="width: 140px;
              height: 100px;
              text-align: center;
              display: flex;
              align-items: center;
              justify-content: center;
              background-color: white;">无</div>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="250" />
        <el-table-column prop="updateTime" label="修改时间" width="250" />

        <el-table-column label="操作" align="center" width="200" fixed="right">
          <template #default="scope">
            <el-button type="success" link @click.stop="viewSubuser(scope.row)">
              查看子用户
            </el-button>
            <el-button type="success" link @click.stop="handleUpdate(scope.row)">
              编辑
            </el-button>

            <el-button type="danger" link @click.stop="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- pagination -->
      <pagination v-if="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
  </div>
  <!-- 子用户列表的弹窗 -->
  <el-dialog v-model="viewSubuserVisible" title="子用户列表">
    <template #default>
      <el-button :icon='Plus' @click="addSub">新增</el-button>
      <el-table v-loading="viewSubuserLoading" :data="subuserDataList" row-key="id" default-expand-all :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" @selection-change="handleSelectionChange">
        <el-table-column prop="entrepreneurName" label="企业用户" min-width="200" />
        <el-table-column prop="accountName" label="账户名称" min-width="200" />
        <el-table-column prop="nickname" label="昵称" min-width="200" />
        <el-table-column label="是否主账户" width="140" >
          <template #default="scope">
            {{scope.row.isMain == '1' ? '是' : '否'}}
          </template>
        </el-table-column>
      </el-table>
      <sub-detail @entrepreneurItempages= 'entrepreneurItempages' v-model:dialog='subDialog.visible' :title = 'subDialog.title' :params = 'params'></sub-detail>
    </template>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="viewSubuserVisible = false">确认</el-button>
        <!-- <el-button type="primary" @click="innerVisible = true">
          open the inner Dialog
        </el-button> -->
      </div>
    </template>
  </el-dialog>
  <!-- dialog -->
  <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" @close="closeDialog">
    <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="企业全称" prop="name">
        <el-input v-model="formData.name" placeholder="请填写企业全称"></el-input>
      </el-form-item>
      <el-form-item label="法人代表" prop="delegate">
        <el-input v-model="formData.delegate" placeholder="请填写法人代表"></el-input>
      </el-form-item>
      <el-form-item label="统一信用代码" prop="taxNo">
        <el-input v-model="formData.taxNo" placeholder="请填写统一信用代码"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input v-model="formData.mobile" placeholder="请填写手机号码"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" placeholder="请填写邮箱"></el-input>
      </el-form-item>
      <!-- <el-form-item label="合同" prop="contractId">
        <el-input v-model="formData.contractId" placeholder="请填写地址"></el-input>
      </el-form-item>
      <el-form-item label="银行" prop="bankCardId">
        <el-input v-model="formData.bankCardId" placeholder="请填写银行"></el-input>
      </el-form-item> -->
      <el-form-item label="地址" prop="address">
        <el-input v-model="formData.address" placeholder="请填写地址"></el-input>
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input v-model="formData.idCard" placeholder="请填写身份证"></el-input>
      </el-form-item>
      <!-- <el-form-item label="证件图片正面" prop="delegateImgRx">
        <el-input v-model="formData.delegateImgRx" placeholder="请上传证件图片正面"></el-input>
      </el-form-item>
      <el-form-item label="证件图片反面" prop="delegateImgGh">
        <el-input v-model="formData.delegateImgGh" placeholder="请上传证件图片反面"></el-input>
      </el-form-item> -->
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
</style>
